﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;

namespace 玩具商城管理系统.Department
{
    public partial class reduce_department : Form
    {
        public reduce_department()
        {
            InitializeComponent();
        }
        SqlDataAdapter daDepartment;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from depart";
            daDepartment = new SqlDataAdapter(str, DB.cn);
            daDepartment.Fill(ds, "department_info");
            DB.cn.Close();
        }
        void showAll()
        {
            DataView dvDepartment = new DataView(ds.Tables["department_info"]);
            dgvdepart.DataSource = dvDepartment;
            dgvdepart.Columns[2].Width = 400;
            
        }
        void dgvstyle()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dgvdepart.Columns.Add(acCode);
            
        }
        private void txtdepartdesc_TextChanged(object sender, EventArgs e)
        { }

        private void dgvDepartment_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvdepart.Rows.Count > 0)
            {
                for (int i = 0; i < dgvdepart.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dgvdepart.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }


            }
        }

        
        void UpdateDB()
        {
            try
            {
                SqlCommandBuilder dbDepatment = new SqlCommandBuilder(daDepartment);
                daDepartment.Update(ds, "department_info");
                
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            DB.cn.Close();
        }
        
            private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void reduce_department_Load(object sender, EventArgs e)
        {
            init(); 
            dgvstyle();
            showAll();
           
        }

        private void dgvdepart_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvdepart.Rows.Count > 0)
            {
                for (int i = 0; i < dgvdepart.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dgvdepart.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }


            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int s = dgvdepart.Rows.Count;
            for (int i = 0; i < dgvdepart.Rows.Count; i++)
            {
                if (dgvdepart.Rows[i].Cells["acCode"].EditedFormattedValue.ToString() == "True")
                {
                    DialogResult dr = MessageBox.Show("您确定要对此商品进行删除吗", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.OK)
                    {
                        dgvdepart.Rows.RemoveAt(i);
                        UpdateDB();
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    s = s - 1;

                }
            }
            if (s == 0)
            {
                MessageBox.Show("请选择你要删除的商品");
            }
            DB.GetCn();
            //使用存储过程添加日志
            SqlCommand cmd = new SqlCommand("add_log", DB.cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("username", SqlDbType.NVarChar));
            cmd.Parameters.Add(new SqlParameter("type", SqlDbType.NVarChar));
            cmd.Parameters.Add(new SqlParameter("action_table", SqlDbType.NVarChar));
            cmd.Parameters.Add(new SqlParameter("action_date", SqlDbType.DateTime));

            cmd.Parameters["username"].Value = login.username;
            cmd.Parameters["type"].Value = "删除";
            cmd.Parameters["action_table"].Value = "depart表";
            cmd.Parameters["action_date"].Value = DateTime.Now;
            try
            {
                SqlCommandBuilder dbDepartment = new SqlCommandBuilder(daDepartment);
                daDepartment.Update(ds, "department_info");
                cmd.ExecuteNonQuery();//执行存储过程
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
